package com.shell.customer.base.cmd;

import com.shell.bee.acct.account.po.AcctBas;
import com.shell.bee.base.exception.UnCheckException;
import com.shell.bee.base.utils.JsonUtil;
import com.shell.bee.sequence.SequenceHelper;
import com.shell.customer.base.vo.CusRecVo;
import com.shell.customer.trade.service.CustomerAcctService;
import com.shell.framework.command.CommandContext;
import com.shell.framework.command.ICommand;
import com.shell.plumber.PlumberReturns;
import com.shell.plumber.bank.BankExtFeign;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class CIBBankVirtualAcctCmd implements ICommand<CusRecVo> {
	private static final String BANK_CODE = "bank_cib";
	@Autowired
	private CustomerAcctService customerAcctService;
	@Autowired
	private BankExtFeign bankExtFeign;
	@Value("${cib.acctno.sequence.code}")
	private String cibSequenceCode;
	@Value("${cib.acctno.initial.value}")
	private String cibInitialValue;

	public boolean execute(CommandContext<CusRecVo> context) throws Exception {
		Object coopBankObj = context.getValue("coopBanks");
		if (coopBankObj != null) {
			List<String> coopBanks = (List) coopBankObj;
			if (coopBanks.contains("bank_cib")) {
				CusRecVo cusRec = (CusRecVo) context.getSource();
				String acctNo = SequenceHelper.generateCode(this.cibSequenceCode, 6,
						Long.valueOf(this.cibInitialValue));
				AcctBas bas = this.customerAcctService.openBankVirtualAcct("bank_cib", cusRec, acctNo);
				if (bas == null) {
					return true;
				}
				Map<String, Object> paramMap = new LinkedHashMap();
				paramMap = new LinkedHashMap();
				paramMap.put("acctNo", acctNo);
				paramMap.put("acctName", bas.getAcctNm());
				List<Map<String, Object>> params = new ArrayList();
				params.add(paramMap);
				String paramsJson = JsonUtil.toJson(params);
				PlumberReturns<Map<String, List<String>>> ret = this.bankExtFeign.batchOpen("bank_cib", paramsJson);
				if (!"S".equals(ret.getRspCode())) {
					List<String> errorMsgs = (List) ((Map) ret.getSource()).get("errorMsgs");
					String msg = "";
					if ((errorMsgs != null) && (errorMsgs.size() > 0)) {
						msg = (String) errorMsgs.get(0);
					}
					throw new UnCheckException("����������������������" + msg);
				}
			}
		}
		return true;
	}
}
